package cn.qianyan.controllers;

import cn.qianyan.beans.Area;
import cn.qianyan.beans.Device;
import cn.qianyan.service.RegionalManageService;
import cn.qianyan.service.devManaService;
import cn.qianyan.utils.pageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@ResponseBody
@RequestMapping("regionalManage")
public class RegionalManageController {

    @Autowired
    private RegionalManageService regionalManageService;
    @Autowired
    private devManaService devManaService;
    @RequestMapping("showAllDev")
    public Map showAllDev(@RequestParam("Page_number") int Page_number){
        Map<Object, Object> map = new HashMap<>();
        Map<Object, Object> inMap = new HashMap<>();
        inMap.put("Page_number",Page_number);
        pageBean pageBean = regionalManageService.showAllDev(inMap);
        if (pageBean!=null){
            map.put("code",0);
            map.put("data",pageBean);
        }else {
            map.put("code",100010);
        }
        return map;
    }
    @RequestMapping("selectLikeDev")
    public Map selectLikeDev(@RequestParam("Page_number") int Page_number,
                             @RequestParam("search_info") String search_info){
        Map<Object, Object> map = new HashMap<>();
        Map<Object, Object> inMap = new HashMap<>();
        inMap.put("Page_number",Page_number);
        inMap.put("search_info",search_info);
        pageBean pageBean = regionalManageService.showAllDev(inMap);
        if (pageBean!=null){
            map.put("code",0);
            map.put("data",pageBean);
        }else {
            map.put("code",100011);
        }
        return map;
    }
    @RequestMapping("addRegional")
    public Map addRegional(@RequestParam("aname") String aname,
                           @RequestParam("aplace") String aplace,
                           @RequestParam("amanager") String amanager,
                           @RequestParam("did") String did){
        Map<Object, Object> map = new HashMap<>();
        Device device = new Device();
        Area area = new Area();
        String aid = UUID.randomUUID().toString();
        area.setAid(aid);
        area.setAname(aname);
        area.setAplace(aplace);
        area.setAmanager(amanager);
        device.setDid(did);
        device.setAid(aid);
        int i = regionalManageService.addRegional(area);
        int j = devManaService.updateDev(device);
        if (i>0&&j>0){
            map.put("code",0);
        }else {
            map.put("code",10012);
        }
        return map;
    }
    @RequestMapping("updateAreaDev")
    public Map updateAreaDev(@RequestParam("aname") String aname,
                             @RequestParam("aplace") String aplace,
                             @RequestParam("amanager") String amanager,
                             @RequestParam("aid") String aid,
                             @RequestParam("did") String did){
        Map<Object, Object> map = new HashMap<>();
        Area area = new Area();
        Device device = new Device();
        area.setAid(aid);
        area.setAname(aname);
        area.setAplace(aplace);
        area.setAmanager(amanager);
        device.setDid(did);
        device.setAid(aid);
        int i = regionalManageService.updateAreaDev(area);
        int j = devManaService.updateDev(device);
        if (i>0&&j>0){
            map.put("code",0);
        }else {
            map.put("code",10013);
        }
        return map;
    }
    @RequestMapping("deleteAreaDev")
    public Map deleteAreaDev(@RequestParam("aid") String aid){
        Map<Object, Object> map = new HashMap<>();
        Area area = new Area();
        area.setAid(aid);
        int i = regionalManageService.deleteAreaDev(area);
        if (i>0){
            map.put("code",0);
        }else {
            map.put("code",10014);
        }
        return map;
    }
}
